package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.CouponMapper;
import com.bwie.mapper.CustomerAddressMapper;
import com.bwie.mapper.ServiceItemMapper;
import com.bwie.pojo.Coupon;
import com.bwie.pojo.CustomerAddress;
import com.bwie.pojo.Orders;
import com.bwie.pojo.ServiceItem;
import com.bwie.service.OrdersService;
import com.bwie.mapper.OrdersMapper;
import com.bwie.service.ServiceItemService;
import com.bwie.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author Yoo19
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2025-07-11 15:04:51
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CustomerAddressMapper customerAddressMapper;
    @Autowired
    HttpServletRequest request;
    @Override
    public R subscribeOrder(Orders orders) {
        //生成订单号
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
        String format = dateFormat.format(new Date());
        String s = RandomUtil.randomNumbers(13);
        String code = format + s ;
        orders.setOrderCode(code);
        //根据服务项id查询服务项获取服务项金额
        ServiceItem item = serviceItemMapper.selectOne(new LambdaQueryWrapper<ServiceItem>().eq(ServiceItem::getId, orders.getServiceItemId()));
        //获取优惠卷列表优惠的价格
        Coupon coupon = couponMapper.selectOne(new LambdaQueryWrapper<Coupon>().eq(Coupon::getCustomerId, orders.getCouponId()));
        //获取客户的地址
        CustomerAddress customerAddress = customerAddressMapper.selectOne(new LambdaQueryWrapper<CustomerAddress>().eq(CustomerAddress::getId, orders.getAddressId()));
        orders.setAddress(customerAddress.getAddress());
        //原始金额
        orders.setOriginalAmount(item.getServicePrice());
        //实际金额
        BigDecimal actualAmount =orders.getOriginalAmount().subtract(coupon.getDiscountAmount());
        orders.setActualAmount(actualAmount);

        //生成创建时间和更新时间
        orders.setCreateTime(new Date());
        orders.setUpdateTime(new Date());
        orders.setStatus(0); // 待支付
        //设置客户id
        String token = request.getHeader("token");
        Claims claims = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer customerId = claims.get("customerId", Integer.class);
        orders.setCustomerId(Long.valueOf(customerId));

        ordersMapper.insert(orders);

        return R.OK();
    }
}




